<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

require_once APP_CLIENT_DIR . 'client/inc/KBClientSearchGenerator.php';
require_once APP_CLIENT_DIR . 'client/inc/KBClientSearchModel.php';


class KBClientView_search_list extends KBClientView_common
{
	
	function &execute(&$manager) {
		
		$this->home_link = true;
		$this->meta_title = $this->msg['search_result_msg'];
		$this->category_nav_generate = false;

		$top = (isset($_GET['period'])) ? false : true;
		if(!$top) {
			$arr = $_GET;
			unset($arr['s']);
			foreach($this->controller->query as $v) { unset($arr[$v]); }

			$sign = ($this->controller->mod_rewrite) ? '?' : '&';
			$link = $this->_getLink('search') . $sign . http_build_query($arr);
			$nav = array($link => $this->msg['advanced_search_msg'], $this->msg['search_result_msg']);
		} else {
			$nav = $this->msg['search_result_msg'];
		}
		
		$data = $this->getTop($manager, $nav, $top);
		$data .= $this->getEntryList($manager);
		
		return $data;	
	}
		
	
	// parse data with articles
	function &getEntryList($manager) {
		
		$search_manager = &$this->getSearchManager($manager, $_GET);
		
		$bp = new PageByPage($manager->getSetting('num_entries_per_page'), $_GET);	
		$bp->setMsg($this->msg['page_msg'], $this->msg['record_msg'], $this->msg['record_from_msg']);
		$bp->countAll($search_manager->getEntryCountSql()); 		
		
		$rows = $this->stripVars($search_manager->getEntryList($bp->limit, $bp->offset));
		$article_staff_padding = '0';
		
		$tpl = &new tplTemplatez($this->template_dir . 'search_list.html');
		
		if(!$rows) {
		
			require_once 'eleontev/HTML/BoxMsg.php';
		
			$msg = new BoxMsg();
			$msg->setMsg('body', $this->msg['no_search_result_msg']);
		
			$tpl->tplAssign('msg', $msg->get());
		
		} else {
            $id_occured = array();
			foreach($rows as $k => $row) {
				if (!array_key_exists($row['id'],$id_occured)) { //去除重复的文章
                    $id_occured = $id_occured + array_flip(array($row['id']));

                    $tpl->tplAssign('category_item_img', $this->_getItemImg($manager->is_registered, $row['category_private'], true));
                    $tpl->tplAssign('category_link', $this->_getLink('index', $row['category_id']));

                    $private = ($row['private'] || $row['category_private']);
                    $tpl->tplAssign('item_img', $this->_getItemImg($manager->is_registered, $private));
                    $tpl->tplAssign('entry_link', $this->_getLink('entry', $row['category_id'], $row['id']));
                    $tpl->tplAssign('updated_date', $this->getFormatedDate($row['ts_updated']));


                    $row['body'] = DocumentParser::getSummary($row['body'], 200);

                    if($manager->getSetting('preview_show_date')) {
                        $tpl->tplSetNeeded('row/show_date');
                        $article_staff_padding = '3';
                    }

                    if($manager->getSetting('preview_show_hits')) {
                        $tpl->tplSetNeeded('row/show_hits');
                        $article_staff_padding = '3';
                    }

    /*
                    if($this->isRatingable($manager, $row['ratingable'])) {
                        if($manager->getSetting('preview_show_rating')) {
                            $tpl->tplSetNeeded('row/show_rate');
                            $tpl->tplAssign('rating', $this->_getRating($row['rating']));
                            $article_staff_padding = '3';
                        }
                    }


                    if($this->isCommentable($manager, $row['commentable'])) {
                        if($manager->getSetting('preview_show_comments')) {
                            $tpl->tplSetNeeded('row/show_comments');
                            $article_staff_padding = '3';
                        }
                    }
    */
                    $tpl->tplAssign('article_staff_padding', $article_staff_padding);
                    $tpl->tplParse(array_merge($row, $this->msg), 'row');
                }
			} 	
		}
	
		$tpl->tplAssign('page_by_page', $bp->shortNavigate());
		$tpl->tplAssign('search_result_msg', $this->msg['search_result_msg']);

		$tpl->tplParse();
		
		return $tpl->tplPrint(1);
	}
	
	
	// will be used in quick responce
	function &getEntryListQuickResponce($manager, $values) {
		
		$search_manager = &$this->getSearchManager($manager, $values);
		$search_manager->sql_params_group = 'GROUP BY e.id';
		$rows = $this->stripVars($search_manager->getEntryList(5, 0), 'true', 'xml');
		
		$tpl = &new tplTemplatez($this->template_dir . 'search_list_responce.html');
		
		if(!$rows) {
			return false;
		
		} else {
            $id_occured = array();
			foreach($rows as $k => $row) {
				if (!array_key_exists($row['id'],$id_occured)) {
                    $id_occured = $id_occured + array_flip(array($row['id']));

                    $tpl->tplAssign('category_item_img', $this->_getItemImg($manager->is_registered, $row['category_private'], true));
                    $tpl->tplAssign('category_link', $this->_getLink('index', $row['category_id']));

                    $private = ($row['private'] || $row['category_private']);
                    $tpl->tplAssign('item_img', $this->_getItemImg($manager->is_registered, $private));
                    $tpl->tplAssign('entry_link', $this->_getLink('entry', $row['category_id'], $row['id']));

                    $row['body'] = DocumentParser::getSummary($row['body'], 300);

                    $tpl->tplParse(array_merge($row, $this->msg), 'row');
                }
			} 	
		}
		
		$tpl->tplAssign('msg', $this->getActionMsg('success', 'quick_responce', 'xml'));
		$tpl->tplParse();
		return $tpl->tplPrint(1);
	}	
	
	
	function &getSearchManager(&$manager, $values) {
	
		$search = &new KBClientSearchGenerator($values);
		$search_manager = &new KBClientSearchModel;
		$search_manager->user_role_id = $manager->user_role_id;
		
		$fulltext_sql = $search->getFullSearchSql('none', array($search_manager->tbl->file_entry,
														        $search_manager->tbl->attachment_to_entry));
		
		$sql[] = $fulltext_sql['where'];
		$sql[] = $search->getDateParams();
		
		// category
		if(!empty($search->values['c'])) {
			
			//$c = &$manager->getCategoryModel();
			$categories =& $manager->categories;
			$sql[] = $search->getCategorySql($categories);
		}
	
		$search_manager->setSqlParams('AND ' . implode(' AND ', $sql));
		$search_manager->setSqlParams('AND ' . $manager->getPrivateSql(false));
		$search_manager->setSqlParams('AND ' . $manager->getCategoryRolesSql(false));
		//$search_manager->setSqlParams('AND e_to_cat.is_main = 1');	
		
		$search_manager->setSqlParamsSelect($fulltext_sql['select']);
		$search_manager->setSqlParamsFrom($fulltext_sql['from']);
		$search_manager->setSqlParamsOrder($search->getOrderSql());

		return $search_manager;
	}	
}
?>